package com.seanchenxi.gwt.wordpress.xmlrpc.service.model.impl;

import java.util.HashMap;

import com.seanchenxi.gwt.wordpress.domain.model.Category;
import com.seanchenxi.gwt.wordpress.domain.model.CategoryCreator;
import com.seanchenxi.gwt.xmlrpc.client.model.impl.BaseModelImpl;

public class CategoryImpl extends BaseModelImpl implements Category, CategoryCreator{
	
	/**
	 * Generated id
	 */
	private static final long serialVersionUID = -1406491256181567892L;
	static final String[] CAT_NAME_KEY = {"categoryName","category_name","name"};
	static final String[] CAT_ID_KEY = {"categoryId","category_id"};
	static final String[] CAT_PARENT_ID_KEY = {"parentId","parent_id"};
	
	public CategoryImpl() {
		super();
	}

	public CategoryImpl(HashMap<String, Object> properties) {
		super(properties);
	}

	@Override
	public int getParentId() {
		return get(getCatParentIdKey());
	}

	@Override
	public void setParentId(int parentId) {
		set(getCatParentIdKey(), parentId);
	}

	@Override
	public String getDescription() {
		return get("description");
	}

	@Override
	public void setDescription(String description) {
		set("description", description);
	}

	@Override
	public int getId() {
		return get(getCatIdKey());
	}

	@Override
	public void setId(int categoryId) {
		set(getCatIdKey(), categoryId);
	}

	@Override
	public String getName() {
		return get(getCatNameKey());
	}

	@Override
	public void setName(String categoryName) {
		set(getCatNameKey(), categoryName);
	}

	@Override
	public String getHtmlUrl() {
		return get("htmlUrl");
	}

	@Override
	public void setHtmlUrl(String htmlUrl) {
		set("htmlUrl", htmlUrl);
	}

	@Override
	public String getRssUrl() {
		return get("rssUrl");
	}

	@Override
	public void setRssUrl(String rssUrl) {
		set("rssUrl", rssUrl);
	}

	@Override
	public String getCategoryDescription() {
		return get("categoryDescription");
	}

	@Override
	public void setCategoryDescription(String categoryDescription) {
		set("categoryDescription", categoryDescription);
	}
	
	@Override
	public String getSlug() {
		return get("slug");
	}

	@Override
	public void setSlug(String slug) {
		set("slug", slug);
	}
	
	@Override
	public boolean isPrimary() {
		if(get("isPrimary") == null) return false;
		return get("isPrimary");
	}
	
	@Override
	public void setPrimary(boolean isPrimary) {
		set("isPrimary", isPrimary);
	}
	
	protected String getCatNameKey(){
		for(String nameKey : CAT_NAME_KEY){
			if(getPropertyNames().contains(nameKey)){
				return nameKey;
			}
		}
		return CAT_NAME_KEY[0];
	}
	
	protected String getCatIdKey(){
		for(String idKey :  CAT_ID_KEY){
			if(getPropertyNames().contains(idKey)){
				return idKey;
			}
		}
		return CAT_ID_KEY[0];
	}
	
	protected String getCatParentIdKey(){
		for(String parentIdKey :  CAT_PARENT_ID_KEY){
			if(getPropertyNames().contains(parentIdKey)){
				return parentIdKey;
			}
		}
		return CAT_PARENT_ID_KEY[0];
	}

}
